﻿//-----------------------------------------------------------------------
// <copyright file="MarketplaceReviewRequestor.cs" company="Microsoft Corporation">
//     Copyright (c) Doug Holland, Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.WindowsPhone
{
    using System;
    using System.IO.IsolatedStorage;
    using System.Windows;

    using Microsoft.Phone.Tasks;

    /// <summary>
    /// Requests that the user review the application within the marketplace after an appropriate level of usage.
    /// </summary>
    public sealed class MarketplaceReviewRequestor
    {
        /// <summary>
        /// Provides access to settings stored within isolated storage.
        /// </summary>
        private IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        /// <summary>
        /// Defines a private thread synchronization object for the <see cref="T:MarketplaceReviewRequestor"/> class.
        /// </summary>
        private object synchronizationObject = new object();

        /// <summary>
        /// Initializes a new instance of the <see cref="T:MarketplaceReviewRequestor"/> class.
        /// </summary>
        public MarketplaceReviewRequestor()
        {
            if (!this.ApplicationReviewed)
            {
                this.RequestReview();
            }
        }

        /// <summary>
        /// Gets the <see cref="T:System.DateTime"/> when the application was first launched.
        /// </summary>
        /// <value>The <see cref="T:System.DateTime"/> when the application was first launched.</value>
        private DateTime ApplicationFirstLaunched
        {
            get
            {
                if (!this.settings.Contains("ApplicationFirstLaunched"))
                {
                    this.settings.Add("ApplicationFirstLaunched", DateTime.Today);
                }

                return (DateTime)this.settings["ApplicationFirstLaunched"];
            }
        }

        /// <summary>
        /// Gets the application usage count.
        /// </summary>
        /// <value>The application usage count.</value>        
        private int ApplicationUsageCount
        {
            get
            {
                if (!this.settings.Contains("ApplicationUsageCounter"))
                {
                    this.settings.Add("ApplicationUsageCounter", 1);
                }
                else
                {
                    int count = (int)this.settings["ApplicationUsageCounter"];

                    this.settings.Remove("ApplicationUsageCounter");

                    this.settings.Add("ApplicationUsageCounter", ++count);
                }

                return (int)this.settings["ApplicationUsageCounter"];
            }
        }

        /// <summary>
        /// Gets or sets the number of times the review has been requested.
        /// </summary>
        /// <value>The number of times the review has been requested.</value>
        private int ReviewRequestCount
        {
            get
            {
                if (!this.settings.Contains("ReviewRequestCount"))
                {
                    this.settings.Add("ReviewRequestCount", 1);
                }

                return (int)this.settings["ReviewRequestCount"];
            }

            set
            {
                this.settings.Remove("ReviewRequestCount");

                this.settings.Add("ReviewRequestCount", value);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the application has been reviewed.
        /// </summary>
        /// <value><c>true</c> if the application has been reviewed; otherwise, <c>false</c>.</value>
        private bool ApplicationReviewed
        {
            get
            {
                if (!this.settings.Contains("ApplicationReviewed"))
                {
                    this.settings.Add("ApplicationReviewed", false);
                }

                return (bool)this.settings["ApplicationReviewed"];
            }

            set
            {
                this.settings.Remove("ApplicationReviewed");

                this.settings.Add("ApplicationReviewed", value);
            }
        }

        /// <summary>
        /// Requests the user review the application.
        /// </summary>
        public void RequestReview()
        {
            lock (this.synchronizationObject)
            {
                // only request the review twice, if the user doesn't want to review don't force a negative review.
                if (this.ReviewRequestCount < 3)
                {
                    int threshold = this.ReviewRequestCount * 7;

                    if (this.ApplicationFirstLaunched != DateTime.Today && this.ApplicationUsageCount > threshold)
                    {
                        this.ReviewRequestCount++;

                        MessageBoxResult result = MessageBox.Show("Would you like to write a review of this application in the Windows Phone marketplace?", "marketplace review", MessageBoxButton.OKCancel);

                        if (result == MessageBoxResult.OK)
                        {
                            MarketplaceReviewTask task = new MarketplaceReviewTask();

                            this.ApplicationReviewed = true;

                            task.Show();
                        }
                    }
                }
            }
        }
    }
}
